import QRequest from '@/service/index';
import { generateNameToLevel } from '@/config/constant';
import type {
    BomDataParams,
    BomDataResponse,
    BomItemDTO,
    BomDataRequest,
    BomTreeVO,
    MaterialChooseResponse,
    MaterialParams,
    BomParams
} from './model/type';

/**
 * @: 获取 BOM 数据
 * @param {BomDataParams} params
 * @return {*}
 */
export const doGetBomList = (params: BomDataParams) => {
    return QRequest.get<BomDataResponse>(`/bom/select/page/list`, { params });
};

/**
 * @: 新增 BOM 数据
 * @param {BomDataRequest} data
 * @return {*}
 */
export const doAddBom = (data: BomDataRequest[]) => {
    return QRequest.post(`/bom/save`, data, {
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 编辑 BOM 数据
 * @param {BomDataRequest} data
 * @return {*}
 */
export const doUpdateBom = (data: BomDataRequest) => {
    return QRequest.put(`/bom/update`, data, {
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 删除 BOM 数据
 * @param {number} params
 * @return {*}
 */
export const doDelBom = (data: number[]) => {
    return QRequest.delete(`/bom/delete`, {
        data,
        headers: {
            hideLoading: 1
        }
    });
};

/**
 * @: 获取 BOM 明细
 * @return {*}
 */
export const doGetBomDetail = (params: BomParams) => {
    return QRequest.get<BomItemDTO>(`/bom/select/bom/detail`, {
        params,
        headers: {
            loadingTarget: `#${generateNameToLevel('4')}`
        }
    });
};

/**
 * @: 获取 BOM 物料编码
 * @param {MaterialChooseResponse} params
 * @return {*}
 */
export const doGetBomMaterial = (params: MaterialParams) => {
    return QRequest.get<MaterialChooseResponse>(`/bom/choose/material`, {
        params,
        headers: {
            loadingTarget: `#${generateNameToLevel('3')}`
        }
    });
};

/**
 * @: 获取 BOM 树层级视图
 * @param {MaterialChooseResponse} params
 * @return {*}
 */
export const doGetBomTreeData = (id: string) => {
    return QRequest.get<BomTreeVO>(`/bom/translate/view?id=${id}`, {
        headers: {
            loadingTarget: `#${generateNameToLevel('5')}`
        }
    });
};
